<template>
  <div class="main">
    <!-- 步骤条 -->
    <header>
      <div class="step">
        <div v-for="item in stepList" :key="item.id" class="step-item">
          <span :class="{ active: currentStep >= item.id }">STEP{{ item.id }}</span>
          <span class="step-description"> {{ item.name }} </span>
          <div class="progress-bar" :style="{ width: (currentStep >= item.id ? '100%' : '0%') }"></div>
        </div>
      </div>
    </header>

    <!-- 步骤内容 -->
    <article>
      <div v-if="currentStep === 1">
        <!-- 步骤一内容：选择邮件类型 -->
        <el-radio-group v-model="radio">
          <el-radio label="1" size="large" border>系统公告</el-radio>
          <el-radio label="2" size="large" border>部门公告</el-radio>
          <el-radio label="3" size="large" border>项目公告</el-radio>
        </el-radio-group>
        <section v-if="radio === '2'">
          <el-form label-position="top">
            <el-form-item label='选择部门'>
              <el-select v-model="form.did" class="m-2" placeholder="Select" size="large" style="width: 240px">
                <el-option v-for="item in departList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-form>
        </section>
        <section v-if="radio === '3'">
          <el-form label-position="top">
            <el-form-item label='选择项目'>
              <el-select v-model="form.pid" class="m-2" placeholder="Select" size="large" style="width: 240px">
                <el-option v-for="item in projectList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-form>
        </section>
      </div>

      <div v-if="currentStep === 2">
        <!-- 步骤二内容：填写邮件内容 -->
        <!-- <label>邮件主题：</label>
        <input v-model="mailSubject" placeholder="请输入邮件主题" />
        <label>邮件内容：</label>
        <textarea v-model="mailContent" placeholder="请输入邮件内容"></textarea> -->
        <el-form :model="form" label-position="top">
          <el-form-item label="公告标题">
            <el-input v-model="form.title" />
          </el-form-item>
          <el-form-item label="公告正文">
            <el-input v-model="form.content" resize="none" :autosize="{ minRows: 20, maxRows: 20 }" type="textarea" />
          </el-form-item>
        </el-form>
      </div>
    </article>

    <!-- 操作按钮 -->
    <footer>
      <el-button size="large" @click="prevStep" :disabled="currentStep === 1">上一步</el-button>
      <el-button type="primary" @click="onSubmit" v-if="currentStep === 2">Create</el-button>
      <el-button size="large" @click="nextStep" :disabled="currentStep === 2">下一步</el-button>
    </footer>

  </div>
</template>

<script>
import { post } from '../../../utils/common/api';
export default {
  data() {
    return {
      stepList: [
        { id: 1, name: '选择公告类型' },
        { id: 2, name: '填写公告内容' }
      ],
      currentStep: 1,
      selectedRecipient: [],
      form: {
        did: undefined,
        pid: undefined,
        title: '',
        content: '',
      },
      radio: '1',
      departList: [],
      projectList: [],
      props: { multiple: true }
    };
  },

  watch: {
    radio() {
      this.form.pid = undefined
      this.form.did = undefined
    },
  },

  methods: {
    formDataReset() {
      this.form.pid = undefined
      this.form.did = undefined
      this.form.content = ''
      this.form.title = ''
    },

    async onSubmit() {
      const res = await post('/info/notice/create', this.form)
      this.formDataReset()
      ElNotification({
        title: '公告创建成功',
        message: `您刚刚创建了公告(${res.id})。`,
        type: 'success',
        position: 'bottom-right',
      })
      this.$router.push({ path: '/info/notice/' })
    },

    async nextStep() {
      if (this.currentStep < 2) {
        this.currentStep++;
      }
    },

    prevStep() {
      if (this.currentStep > 1) {
        this.currentStep--;
      }
    },

    async getAllDepartment() {
      const res = await post('/department/all', {})
      this.departList = res.list
    },

    async getUserProjectList() {
      const res = await post('/project/list', {
        uid: this.usePermission.id
      })
      this.projectList = res.list
    },

    async getProjectUserList(id) {
      const res = await post('/project/profile', {
        id
      })
      this.options = res.members.map(item => ({
        value: item.id,
        label: item.name
      }))
    },
  },

  mounted() {
    this.getAllDepartment()
    this.getUserProjectList()
  },
};
</script>

<style scoped>
.main {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}

article {
  max-height: 100%;
  overflow: hidden;
  box-sizing: border-box;
  flex: 1;
  padding: 10px;
}

footer {
  height: 40px;
  display: flex;
  /* align-items: center; */
  justify-content: space-around;
}

section {
  margin-top: 10px;
  display: flex;
  justify-content: center;
}

/* .step {
  display: flex;
  justify-content: space-between;
}

.step div {
  flex: 1;
  text-align: center;
  padding: 10px;
  border: 1px solid #ccc;
  background-color: #f0f0f0;
}

.step .active {
  background-color: #4caf50;
  color: #fff;
} */

.step {
  display: flex;
  justify-content: space-around;
  margin-bottom: 20px;
}

.step-item {
  text-align: center;
  /* position: relative; */
}

.step-item span {
  font-weight: bold;
}

.step-description {
  margin-left: 10px;
}

.step-item .progress-bar {
  height: 5px;
  width: 100%;
  /* background-color: #ccc; */
  background-color: #3498db;
  /* position: absolute; */
  /* top: 20px; */
  left: 0;
  transition: width 0.3s ease-in-out;
}

.step-item .progress-bar.active {
  background-color: #3498db;
  /* 蓝色 */
}

.el-radio.is-bordered.el-radio--large {
  padding: 0 40px 0 40px;
}
</style>
